from flask import g, jsonify, request

from app2 import db
from app2.models.models import Collection, Comment, CommentLike
from app2.utils.common.common import login_user_data2
from . import news_blue


@news_blue.route("/collect", methods=['POST'])
@login_user_data2
def collect():
    """文章收藏或取消收藏"""
    # print("-"*80)
    # 获取数据
    user = g.user
    news_id = request.json.get("news_id")
    action = request.json.get("action")

    print(news_id, action)

    # 判断是否登录
    if not user:
        ret = {
            "errno": 5002,
            "errmsg": "未登陆"
        }
        # print('未登陆')
        return jsonify(ret)

    # 参数是否完整
    if not all([news_id, action]):
        ret = {
            "errno": 5001,
            "errmsg": "缺少参数"
        }
        return jsonify(ret)

    # 判断do 与 undo
    # 收藏
    if action == "do":
        try:
            collection = Collection(user_id=user.id, news_id=news_id)
            db.session.add(collection)
            db.session.commit()
            ret = {
                "errno": 0,
                "errmsg": "收藏成功"
            }
            return jsonify(ret)
        except Exception as e:
            print(e)  # 若有错误，直接打印错误
            ret = {
                "errno": 5004,
                "errmsg": "收藏失败"
            }
            return jsonify(ret)
    # 取消收藏
    elif action == "undo":
        try:
            collect = db.session.query(Collection).filter(Collection.user_id == user.id, Collection.news_id == news_id).first()
            if collect:
                db.session.delete(collect)
                db.session.commit()
                ret = {
                    "errno": 0,
                    "errmsg": "取消收藏成功"
                }
            else:
                ret = {
                    "errno": 5005,
                    "errmsg": "取消收藏失败"
                }
        except Exception as e:
            print('错误信息为', e)
            ret = {
                "errno": 5005,
                "errmsg": "取消收藏失败"
            }

        return jsonify(ret)


@news_blue.route("/comment", methods=['POST'])
@login_user_data2
def comment():
    """提交评论"""
    # 获取数据
    user = g.user
    news_id = request.json.get("news_id")
    content = request.json.get("content")
    parent_id = request.json.get("parent_id")
    # print(news_id, content)

    # 判断是否登录
    if not user:
        ret = {
            "errno": 5002,
            "errmsg": "未登陆"
        }
        return jsonify(ret)

    # 参数是否完整
    if not all([news_id, content]):
        ret = {
            "errno": 5001,
            "errmsg": "缺少参数"
        }
        return jsonify(ret)

    # 提交评论
    try:
        comment = Comment(user_id=user.id, news_id=news_id, content=content)
        if parent_id:
            comment.parent_id = parent_id
        db.session.add(comment)
        db.session.commit()
        ret = {
            "errno": 0,
            "errmsg": "评论成功"
        }
        return jsonify(ret)

    except Exception as e:
        print("try错误信息", e)
        ret = {
            "errno": 5006,
            "errmsg": "评论失败"
        }
    return jsonify(ret)


@news_blue.route("/comment_like", methods=['POST'])
@login_user_data2
def comment_like():
    """点赞&取点"""
    # 获取数据
    user = g.user
    action = request.json.get("action")
    comment_id = request.json.get("comment_id")
    # 查出需要点赞的该条评论
    comment = db.session.query(Comment).filter(Comment.id == comment_id).first()

    # 判断是否登录
    if not user:
        ret = {
            "errno": 5002,
            "errmsg": "未登陆"
        }
        print('未登陆')
        return jsonify(ret)

    # 参数是否完整
    if not all([action, comment_id]):
        ret = {
            "errno": 5001,
            "errmsg": "缺少参数"
        }
        return jsonify(ret)

    # 判断action

        # 对该评论点赞
    try:
        if action == "do":
            comment_like = CommentLike(user_id=user.id, comment_id=comment_id)
            db.session.add(comment_like)
            comment.like_count += 1
        elif action == "undo":
            comment_like = db.session.query(CommentLike).filter(CommentLike.user_id == user.id, CommentLike.comment_id == comment_id).first()
            db.session.delete(comment_like)
            comment.like_count -= 1
        db.session.commit()
        ret = {
            "errno": 0,
            "errmsg": "操作成功"
        }
    except Exception as e:
        print("try错误信息", e)
        ret = {
            "errno": 5007,
            "errmsg": "操作失败"
        }
    return jsonify(ret)




